package bb.lanxing.lib.devices.base;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.content.Intent;

import bb.lanxing.lib.devices.ble.BleCons;
import bb.lanxing.lib.devices.core.ble.AbsBleDevice;
import bb.lanxing.lib.devices.core.ble.BaseBluetoothGattCallback;
import bb.lanxing.lib.devices.core.ble.SpecificationAttributes;
import java.util.UUID;

public class SimpleBluetoothGattCallback<CLIENT extends AbsBleDevice> extends BaseBluetoothGattCallback<CLIENT> {
    public SimpleBluetoothGattCallback(CLIENT client) {
        super(client);
    }

    @Override
    public void onCharacteristicRead(BluetoothGatt bluetoothGatt, BluetoothGattCharacteristic bluetoothGattCharacteristic, int i) {
        super.onCharacteristicRead(bluetoothGatt, bluetoothGattCharacteristic, i);
        UUID uuid = bluetoothGattCharacteristic.getUuid();
        if (uuid.equals(SpecificationAttributes.BLE_BATTERY_CHARACTERISTIC)) {
            onReadBattery(bluetoothGattCharacteristic.getIntValue(BluetoothGattCharacteristic.FORMAT_UINT8, 0));
        } else if (uuid.equals(SpecificationAttributes.BLE_DEVICE_INFORMATION_FIRMWARE_CHARACTERISTIC)) {
            onReadFirmware(bluetoothGattCharacteristic.getStringValue(0).trim());
        } else if (uuid.equals(SpecificationAttributes.BLE_DEVICE_INFORMATION_MANUFACTURER_CHARACTERISTIC)) {
            onReadManufactureString(bluetoothGattCharacteristic.getStringValue(0).trim());
        } else if (uuid.equals(SpecificationAttributes.BLE_DEVICE_INFORMATION_MODEL_CHARACTERISTIC)) {
            onModelString(bluetoothGattCharacteristic.getStringValue(0).trim());
        } else if (uuid.equals(SpecificationAttributes.BLE_GAP_DEVICENAME_CHARACTERISTIC)) {
            //onReadFirmware(bluetoothGattCharacteristic.getStringValue(0).trim()); //for debug
        }
    }

    protected void onReadBattery(int i) {
        if (this.mClientRef != null) {
            this.mClientRef.setBattery(i);
        }
        Context applicationContext = DeviceContext.getApplicationContext();
        if (applicationContext == null) {
            return;
        }
        Intent intent = new Intent(BleCons.ACTION_BATTERY);
        intent.putExtra(BleCons.EXTRA_DEVICE_TYPE, this.mClientRef.getType());
        intent.putExtra(BleCons.EXTRA_BATTERY, i);
        intent.putExtra(BleCons.EXTRA_DEVICE_ADDRESS, this.mClientRef.getAddress());
        intent.setPackage(DeviceContext.getApplicationContext().getPackageName());
        applicationContext.sendBroadcast(intent);
        CLIENT client = this.mClientRef;
        client.d("Battery: " + i);
    }

    protected void onReadFirmware(String str) {
        if (this.mClientRef != null) {
            this.mClientRef.setFirmwareVersion(str);
        }
        Context applicationContext = DeviceContext.getApplicationContext();
        if (applicationContext == null) {
            return;
        }
        Intent intent = new Intent(BleCons.ACTION_DEVICE_INFORMATION);
        intent.putExtra(BleCons.EXTRA_DEVICE_TYPE, this.mClientRef.getType());
        intent.putExtra(BleCons.EXTRA_FIRMWARE_VERSION, str);
        intent.putExtra(BleCons.EXTRA_DEVICE_ADDRESS, this.mClientRef.getAddress());
        intent.setPackage(DeviceContext.getApplicationContext().getPackageName());
        applicationContext.sendBroadcast(intent);
        CLIENT client = this.mClientRef;
        client.d("Firmware: " + str);
    }

    protected void onReadManufactureString(String str) {
        if (this.mClientRef != null) {
            this.mClientRef.setManufacturer(str);
        }
        Context applicationContext = DeviceContext.getApplicationContext();
        if (applicationContext == null) {
            return;
        }
        Intent intent = new Intent(BleCons.ACTION_DEVICE_INFORMATION);
        intent.putExtra(BleCons.EXTRA_DEVICE_TYPE, this.mClientRef.getType());
        intent.putExtra(BleCons.EXTRA_MANUFACTURE, str);
        intent.putExtra(BleCons.EXTRA_DEVICE_ADDRESS, this.mClientRef.getAddress());
        intent.setPackage(DeviceContext.getApplicationContext().getPackageName());
        applicationContext.sendBroadcast(intent);
        CLIENT client = this.mClientRef;
        client.d("Manufacture: " + str);
    }

    protected void onModelString(String str) {
        if (this.mClientRef != null) {
            this.mClientRef.setModel(str);
        }
        Context applicationContext = DeviceContext.getApplicationContext();
        if (applicationContext == null) {
            return;
        }
        Intent intent = new Intent(BleCons.ACTION_DEVICE_INFORMATION);
        intent.putExtra(BleCons.EXTRA_DEVICE_TYPE, this.mClientRef.getType());
        intent.putExtra(BleCons.EXTRA_MODEL, str);
        intent.putExtra(BleCons.EXTRA_DEVICE_ADDRESS, this.mClientRef.getAddress());
        intent.setPackage(DeviceContext.getApplicationContext().getPackageName());
        applicationContext.sendBroadcast(intent);
        CLIENT client = this.mClientRef;
        client.d("Model: " + str);
    }
}
